<template>
  <div>
    <div class="filter-container _formStyle_" style="padding: 10px 0">
      <el-form ref="form" :inline="true" :model="form" label-width="75px">
        <el-form-item label="配置项:" label-width="60px">
          <el-input
            v-model.trim="form.configItem"
            style="width: 160px"
            clearable
          />
        </el-form-item>
        <el-form-item label="配置描述:">
          <el-input
            v-model.trim="form.configDesc"
            style="width: 160px"
            clearable
          />
        </el-form-item>
        <el-form-item>
          <el-form-item>
            <el-button
              class="filter-item"
              type="primary"
              icon="el-icon-search"
              @click="handleFilter"
              >查询</el-button
            >
            <el-button
              class="filter-item"
              style="margin-left: 10px"
              type="primary"
              icon="el-icon-edit-outline"
              @click="handleReset"
              >重置</el-button
            >
          </el-form-item>
        </el-form-item>
      </el-form>
    </div>
    <el-table height="680" :data="list" highlight-current-row>
      <el-table-column
        prop="id"
        header-align="center"
        align="center"
        label="id"
      />
      <el-table-column
        prop="configItem"
        header-align="center"
        align="center"
        label="配置项"
      />
      <el-table-column
        prop="configDesc"
        header-align="center"
        align="center"
        label="配置描述"
      />
      <el-table-column
        prop="configValue"
        header-align="center"
        align="center"
        label="配置内容"
      />
      <el-table-column
        header-align="center"
        align="center"
        width="150"
        label="操作"
      >
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="handleUpdate(scope.row)"
            >编辑</el-button
          >
          <el-button type="text" size="small" @click="deleteHandle(scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <el-dialog
      :title="editTitle"
      :visible.sync="dialogVisible"
      width="50%"
      :close-on-click-modal="false"
      v-if="dialogVisible"
    >
      <keep-alive>
        <component
          :is="currentComponent"
          :editDate="editDate"
          @close="handleClose"
        ></component>
      </keep-alive>
    </el-dialog>
  </div>
</template>

<script>
import { jlcpCodeConfigFindPage, jlcpCodeConfigDelete } from "@/api/lowcode";
import keySettingsComponents from "./keySettingsComponents.vue";
import notificationComponents from "./notificationComponents.vue";
import addconfigurationComponents from "./addconfigurationComponents.vue";
import addSignature from "./addSignature.vue";
import emailSettings from "./emailSettings.vue";
export default {
  name: "notificationListComponents",
  components: {
    keySettingsComponents,
    notificationComponents,
    addconfigurationComponents,
    addSignature,
    emailSettings,
  },
  data() {
    return {
      currentComponent: "customQuery",
      editDate: {},
      dialogVisible: false,
      editTitle: "查询组件编辑",
      // 筛选框的 form 表单
      form: {
        projectId: +sessionStorage.getItem("developLowCodeId"),
        configItem: "",
        configDesc: "",
      },
      list: [], // 表格数据
    };
  },
  // async created() {
  //   this.getList();
  // },
  //由于该组件添加了keep-alive缓存，所以每次进入这个组件调用activated生命周期
  activated() {
    this.getList();
  },
  methods: {
    handleClose(flag) {
      this.dialogVisible = false;
      if (flag) {
        this.getList();
      }
    },
    // 获取表格数据
    async getList() {
      this.list = [];
      const res = await jlcpCodeConfigFindPage(this.form);
      if (res?.code === 200) {
        this.list = res.data;
      }
    },
    // 重新获取表格数据 页码归为1
    handleFilter() {
      // this.form.page = 1;
      this.getList();
    },
    handleReset() {
      this.form = {
        projectId: +sessionStorage.getItem("developLowCodeId"),
        configItem: "",
        configDesc: "",
      };
      this.getList();
    },
    handleUpdate(row) {
      this.dialogVisible = true;
      this.editDate = JSON.parse(JSON.stringify(row));
      if (
        row.configType === "SMS_MODULE" ||
        row.configType === "EMAIL_MODULE"
      ) {
        this.editTitle = `${
          this.editDate.configType === "SMS_MODULE" ? "短信" : "邮件"
        }通知模板编辑`;
        this.currentComponent = "notificationComponents";
      } else if (row.configType === "COMMON") {
        this.editTitle = "配置项编辑";
        this.currentComponent = "addconfigurationComponents";
      } else if (row.configType === "SMS_SIGN_NAME") {
        this.editTitle = "短信签名编辑";
        this.currentComponent = "addSignature";
      } else if (row.configType === "SMS_ACCESS") {
        this.editTitle = "短信通知密钥设置编辑";
        this.currentComponent = "keySettingsComponents";
        this.editDate.configValue = JSON.parse(this.editDate.configValue);
      } else if (row.configType === "EMAIL") {
        this.editTitle = "邮件通知设置";
        this.currentComponent = "emailSettings";
        this.editDate.configValue = JSON.parse(this.editDate.configValue);
      }
    },
    async deleteHandle(item) {
      this.$confirm(`确定要进行删除操作?`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(async () => {
        const res = await jlcpCodeConfigDelete([item.id]);
        if (res?.code === 200) {
          this.$message({
            message: "删除成功",
            type: "success",
          });
          this.getList();
        }
      });
    },
  },
};
</script>
<style lang="scss" scoped>
@import "~@/styles/layout-table.scss";
$rowLength: 2;
@media all and (min-width: 1659px) {
  $rowLength: 2;
  @include calculation($rowLength: $rowLength);
}
@media all and (max-width: 1659px) {
  $rowLength: 3;
  @include calculation($rowLength: $rowLength);
}
._formStyle_ ::v-deep .el-form-item__label {
  text-align: left;
  margin-top: 4px;
}
</style>
